﻿#include "record.h"
#include "ui_record.h"
#include <QMessageBox>
#include "lib/sql_mgr.h"
#include"dlg_bookrb.h"
Record::Record(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Record)
{
    ui->setupUi(this);
    ui->tableView->setModel(&m_Model);//绑定模型
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
}

Record::~Record()
{
    delete ui;
}
void Record::initPage(QString strCondtion)
{
    auto l = sql_mgr::getInstance()->getRecord(strCondtion);
    m_Model.clear();
    m_Model.setHorizontalHeaderLabels(QStringList{"ISBN","Borrower_Id","DeadLine"});
    for(int i = 0;i<l.size();i++)
    {
        QList<QStandardItem*> items;
        for(int j = 0;j<l[i].size();j++)
        {
            items.append(new QStandardItem(l[i][j]));
        }

        m_Model.appendRow(items);
    }
}
void Record::on_btn_back_clicked()
{
    int r = ui->tableView->currentIndex().row();
    if(r<0)
    {
        return;
    }
    auto id = m_Model.item(r,0)->text();

    dlg_bookrb dlg;
    dlg.setType(true);
    dlg.setBookId(id);
    int ret = dlg.exec();
    QMessageBox::information(nullptr,"Message",ret?"Succeed":"Failure");
    if(ret)
    {
        initPage();
    }
}


void Record::on_lineEdit_textChanged(const QString &arg1)
{
    QString strCond = QString("where ISBN like '%%1%' or Borrower_Id like '%%1%'").arg(arg1);
initPage(strCond);
}


void Record::on_btn_clear_clicked()
{
    sql_mgr::getInstance()->clearRecord();
    initPage();
}

